/**
 * @author mithun
 */

//var console = window.loadFirebugConsole();

// Global variables	
var selectedNodeIds  = '';
var selectedPoiIds   = '';
var currentViaNode;

// init function called on window load
Event.observe(window, 'load', function(event) {
	
	// Initiate currentViaNode
	currentViaNode = $('RouteNodeId').value;
	
});	

var addViaNodeToSelectedNodes = function(el) {
	
	var flag = false;
	var i = 0;
	var newViaNode = el.value;
	
	// Get the 'selected-node' list and 'available-node' list.
	var selectedNodeDivs = $('selectedNodes').getElementsBySelector('div');
	var availableNodeDivs = $('availableNodes').getElementsBySelector('div');
	
	// Remove the old via node from 'selected-nodes'
	for (i = 0; i < availableNodeDivs.length; i++) {
		var nodeId = availableNodeDivs[i].id.replace("nodeItem_", '');
		if (nodeId == currentViaNode) {
			if (hasClassName(availableNodeDivs[i], 'highlight')) {
				// remove
				toggleNode(availableNodeDivs[i]);
			}
		}
	}
	
	// check whether newly selected via node is already present in the selected node list
	flag = false;
	for (i = 0; i < selectedNodeDivs.length; i++) {
		var nodeId = selectedNodeDivs[i].id.replace("nodeItem_", '');
		if (el.value == nodeId) {
			// Already in selected nodes table
			flag = true;
			break;
		}
	}
		
	if (!flag) {
		
		// Find via node from available node table
		flag = false;
		for (var i = 0; i < availableNodeDivs.length; i++) {
			var nodeId = availableNodeDivs[i].id.replace("nodeItem_", '');
			if (newViaNode == nodeId) {
				// Found via node
				flag = true;
				break;
			}
		}
		if (flag) {
			// Add newly selected via node to 'selected-nodes'
			var viaNode = availableNodeDivs[i];
			if (hasClassName(viaNode, 'highlight')) {
				// Via node already in selected list
			} else {
				// Add via node to selected list
				toggleNode(viaNode);
			}
		}	
		
	}
	
	// Restore newly selected via node value if it is changed to none
	
	var options =  $('RouteNodeId').options;
	for(i = 0; i < options.length; i++) {
		if (options[i].value == newViaNode) {
			$('RouteNodeId').selectedIndex = i;
		}
	}
	
	// save value of newly selected via node for future use
	currentViaNode = $('RouteNodeId').value;
	
	
}

var submitForm = function(el) {
	
	// Add selected nodes to route form
	var selectedNodeDivs = $('selectedNodes').getElementsBySelector('div');
	var viaNode = $('RouteNodeId');
	var viaNodePresent = false;
	
	for (var i =0 ; i< selectedNodeDivs.length; i++) {
		var nodeId    = selectedNodeDivs[i].id.replace("nodeItem_", ''); 
		selectedNodeIds += nodeId+"&"+(i+1);
		if(i != selectedNodeDivs.length-1){
			selectedNodeIds += ";";
		} 
		if (nodeId == viaNode.value) {
			viaNodePresent = true;
		}     
	}
	if (!selectedNodeIds) {
		alert("Please select some nodes for this route.");
		return false;
	}
	if (!viaNodePresent && !viaNode.value) {
		alert("Via node should be included in the selected node list.");
		return false;
	}
	
	var nodeIdsField = document.createElement('INPUT');
	nodeIdsField.setAttribute('type', 'hidden');
	nodeIdsField.setAttribute('name', 'data[Node][ids]');
	nodeIdsField.value = selectedNodeIds;
	document.getElementsByTagName('form')[0].appendChild(nodeIdsField);
	
	// Add selected pois to route form
	var selectedPoiDivs = $('selectedPois').getElementsBySelector('div');
	for (var i=0; i< selectedPoiDivs.length; i++) {
		var poiId    = selectedPoiDivs[i].id.replace("poiItem_", ''); 
		selectedPoiIds += poiId+"&"+(i+1);
			if(i != selectedPoiDivs.length-1){
				selectedPoiIds += ";";
			}      
	}
	
	var poiIdsField = document.createElement('INPUT');
	poiIdsField.setAttribute('type', 'hidden');
	poiIdsField.setAttribute('name', 'data[PointOfInterest][ids]');
	poiIdsField.value = selectedPoiIds;
	document.getElementsByTagName('form')[0].appendChild(poiIdsField);
	
	// Submit form
	submitFormViaJavaScript(el.form);
	return true;
	
}

var toggleNode = function(el) {
	
	
//	if (el.hasClassName('highlight')) {
//		el.removeClassName('highlight');
//		cloneDiv.addClassName('selected');
//		tableDnD.init($('selectedNodes'));

	if (hasClassName(el, 'selected')) {
		return;
	}
		
	if (hasClassName(el, 'highlight')) {
		
		removeClassName(el, 'highlight');
		var selectedNodeDivs = $('selectedNodes').getElementsBySelector('div');
		for (var i = 0; i < selectedNodeDivs.length; i++) {
			if (el.id == selectedNodeDivs[i].id) {
				$('selectedNodes').removeChild(selectedNodeDivs[i]);
			}
		}
		var nodeId = el.id.replace("nodeItem_", '');
		// If el is the current via node, unselect the via node drop-down
		if (currentViaNode == nodeId) {
			$('RouteNodeId').selectedIndex = 0;
			currentViaNode = $('RouteNodeId').value;
		}
		 
		
	} else {
	
		addClassName(el, 'highlight');
		cloneDiv = el.cloneNode(true);
		addClassName(cloneDiv, 'selected');
		cloneDiv.removeChild(cloneDiv.lastChild);
		cloneDiv.innerHTML = cloneDiv.innerHTML+"<span class='crossButton' onclick='removeNodeFromRoute(this);'>&nbsp;&nbsp;&nbsp;&nbsp;</span>"; 
		cloneDiv.onclick = function () {
			toggleSelectedItem(this);
		};
		$('selectedNodes').appendChild(cloneDiv);
		$('selectedNodes').scrollTop = $('selectedNodes').scrollHeight;
		
	}
	
}

var togglePoi = function(el) {
	
	
	if (hasClassName(el, 'selected')) {
		return;
	}
		
	if (hasClassName(el, 'highlight')) {
		removeClassName(el, 'highlight');
		var selectedPoiDivs = $('selectedPois').getElementsBySelector('div');
		for (var i = 0; i < selectedPoiDivs.length; i++) {
			if (el.id == selectedPoiDivs[i].id) {
				$('selectedPois').removeChild(selectedPoiDivs[i]);
			}
		}
		
	} else {
		addClassName(el, 'highlight');
		cloneDiv = el.cloneNode(true);
		addClassName(cloneDiv, 'selected');
		cloneDiv.removeChild(cloneDiv.lastChild);
		cloneDiv.innerHTML = cloneDiv.innerHTML+"<span class='crossButton' onclick='removePoiFromRoute(this);'>&nbsp;&nbsp;&nbsp;&nbsp;</span>"; 
		cloneDiv.onclick = function () {
			toggleSelectedItem(this);
		};
		$('selectedPois').appendChild(cloneDiv);
		$('selectedPois').scrollTop = $('selectedPois').scrollHeight;
	}
	
}

var toggleSelectedItem = function(el) {
	
	if (hasClassName(el, 'move-select')) {
		removeClassName(el, 'move-select');
	} else {
		addClassName(el, 'move-select');
	}
	
}

var removeNodeFromRoute = function(el) {
	
	var availableNodeDivs = $('availableNodes').getElementsBySelector('div');
	flag = false;
	for (var i = 0; i < availableNodeDivs.length; i++) {
		if (el.parentNode.id == availableNodeDivs[i].id) {
			toggleNode(availableNodeDivs[i]);
		}
	}
	
}

var removePoiFromRoute = function(el) {
	
	var availablePoiDivs = $('availablePois').getElementsBySelector('div');
	flag = false;
	for (var i = 0; i < availablePoiDivs.length; i++) {
		if (el.parentNode.id == availablePoiDivs[i].id) {
			togglePoi(availablePoiDivs[i]);
		}
	}
	
}

var moveSelectedNodesDown = function() {
	
	var selectedNodeDivs = $('selectedNodes').getElementsBySelector('div');
	var nodesCloneArray  = new Array();
	var cloneArrayIndex = 0;
	var tmp = null;
	var i = 0;
	for (i = 0; i < selectedNodeDivs.length; i++) {
		nodesCloneArray[cloneArrayIndex++] = selectedNodeDivs[i].cloneNode(true);
	}

	for (i = nodesCloneArray.length-2; i >= 0; i--) {
		if (hasClassName(nodesCloneArray[i], 'move-select') && !hasClassName(nodesCloneArray[i+1], 'move-select')) {
			tmp = nodesCloneArray[i];
			nodesCloneArray[i]   = nodesCloneArray[i+1];
			nodesCloneArray[i+1] = tmp;
		}
	}
	
	$('selectedNodes').innerHTML = "";
	for (i = 0; i < nodesCloneArray.length; i++) {
		nodesCloneArray[i].onclick = function () {
			toggleSelectedItem(this);
		};
		$('selectedNodes').appendChild(nodesCloneArray[i]);
	}
	
}

var moveSelectedNodesUp = function() {
	
	var selectedNodeDivs = $('selectedNodes').getElementsBySelector('div');
	var nodesCloneArray  = new Array();
	var cloneArrayIndex = 0;
	var tmp = null;
	var i = 0;
	
	for (i = 0; i < selectedNodeDivs.length; i++) {
		nodesCloneArray[cloneArrayIndex++] = selectedNodeDivs[i].cloneNode(true);
	}

	for (i = 1; i < nodesCloneArray.length; i++) {
		if (hasClassName(nodesCloneArray[i], 'move-select') && !hasClassName(nodesCloneArray[i-1], 'move-select')) {
			tmp = nodesCloneArray[i];
			nodesCloneArray[i]   = nodesCloneArray[i-1];
			nodesCloneArray[i-1] = tmp;
		}
	}
	
	$('selectedNodes').innerHTML = "";
	for (i = 0; i < nodesCloneArray.length; i++) {
		nodesCloneArray[i].onclick = function () {
			toggleSelectedItem(this);
		};
		$('selectedNodes').appendChild(nodesCloneArray[i]);
	}
	
}

var moveSelectedPoisDown = function() {
	
	var selectedPoiDivs = $('selectedPois').getElementsBySelector('div');
	var poisCloneArray  = new Array();
	var cloneArrayIndex = 0;
	var tmp = null;
	var i = 0;
	for (i = 0; i < selectedPoiDivs.length; i++) {
		poisCloneArray[cloneArrayIndex++] = selectedPoiDivs[i].cloneNode(true);
	}

	for (i = poisCloneArray.length-2; i >= 0; i--) {
		if (hasClassName(poisCloneArray[i], 'move-select') && !hasClassName(poisCloneArray[i+1], 'move-select')) {
			tmp = poisCloneArray[i];
			poisCloneArray[i]   = poisCloneArray[i+1];
			poisCloneArray[i+1] = tmp;
		}
	}
	
	$('selectedPois').innerHTML = "";
	for (i = 0; i < poisCloneArray.length; i++) {
		poisCloneArray[i].onclick = function () {
			toggleSelectedItem(this);
		};
		$('selectedPois').appendChild(poisCloneArray[i]);
	}
	
}

var moveSelectedPoisUp = function() {
	
	var selectedPoiDivs = $('selectedPois').getElementsBySelector('div');
	var poisCloneArray  = new Array();
	var cloneArrayIndex = 0;
	var tmp = null;
	var i = 0;
	
	for (i = 0; i < selectedPoiDivs.length; i++) {
		poisCloneArray[cloneArrayIndex++] = selectedPoiDivs[i].cloneNode(true);
	}

	for (i = 1; i < poisCloneArray.length; i++) {
		if (hasClassName(poisCloneArray[i], 'move-select') && !hasClassName(poisCloneArray[i-1], 'move-select')) {
			tmp = poisCloneArray[i];
			poisCloneArray[i]   = poisCloneArray[i-1];
			poisCloneArray[i-1] = tmp;
		}
	}
	
	$('selectedPois').innerHTML = "";
	for (i = 0; i < poisCloneArray.length; i++) {
		poisCloneArray[i].onclick = function () {
			toggleSelectedItem(this);
		};
		$('selectedPois').appendChild(poisCloneArray[i]);
	}
	
}

var hasClassName = function(el, className) {
	
	if (Prototype.Browser.IE) {
		if (el.className.match(className)) {
			return true;	
		} else {
			return false;
		}
	} else {
		return el.hasClassName(className);
	}
	
}

var addClassName = function(el, className) {
	
	if (Prototype.Browser.IE) {
		if (el.className.match(className) == null) {
			el.className = el.className + ' ' + className;	
		}
	} else {
		el.addClassName(className);
	}
	
}

var removeClassName = function(el, className) {
	
	if (Prototype.Browser.IE) {
		el.className = el.className.replace(className,"");
	} else {
		el.removeClassName(className);
	}
	
}